<template>
    <div class="box">
        <input type="text" v-model="input1" />
        <h2>Son1 子组件</h2>
        从vuex中获取的值: <label>{{ $store.state.count }}</label>
        <br>
        <button @click="handleAdd">值 + 1</button>
        <button @click="handleFive">值 + 5</button>
        <button @click="changeTitle">改标题</button>
        <button @click="handleAddWithParam(3)">值 + 3</button>
        <button @click="handleAddWithParam(6)">值 + 6</button>
        <button @click="handleAddWithParam(9)">值 + 9</button>
        <button @click="changeTitleWithParam">改标题(输入)</button>
        <button @click="handleChangeActions">一秒后修改为输入框的值</button>

        <hr>
        <div>{{ $store.state.list }}</div>
        <div>直接使用 {{ $store.getters.filterList }}</div>
        <div>mapGetters {{ filterList }}</div>
    </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Son1Com',
  data () {
    return {
      input1: ''
    }
  },
  computed: {
    ...mapGetters(['filterList'])
  },
  methods: {
    handleAdd () {
      // 错误代码(vue默认不会监测，监测需要成本)
      // this.$store.state.count++
      // console.log(this.$store.state.count) I
      // *****应该通过 mutation 核心概念，进行修改数据
      this.$store.commit('addCount')
    },
    handleFive () {
      this.$store.commit('addFive')
    },
    changeTitle () {
      this.$store.commit('changeTitle')
    },
    handleAddWithParam (n) {
      this.$store.commit('handleAddWithParam', n)
    },
    changeTitleWithParam () {
      this.$store.commit('changeTitleWithParam', this.input1)
    },
    handleChangeActions () {
      // 调用action
      this.$store.dispatch('changeCountAsync', this.input1)
    }
  }
}
</script>

<style lang="css" scoped>
.box {
    border: 3px solid #ccc;
    width: 400px;
    padding: 10px;
    margin: 20px;
}

h2 {
    margin-top: 10px;
}
</style>
